//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Stock.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Quote.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CStock::CStock (Cephei::QL::IQuote^ quote, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CStock::typeid)
{
    CQuote^ _Cquote;
    try
    {
#ifdef HANDLE
        _phStock = NULL;
#endif
        _Cquote = safe_cast<CQuote^> (quote);
        _Cquote->Lock();
        Handle<QuantLib::Quote>& _quote = static_cast<Handle<QuantLib::Quote>&> (_Cquote->GetHandle ()); 
        _ppStock = new boost::shared_ptr<QuantLib::Stock> (new QuantLib::Stock ( _quote ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppStock)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppStock));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cquote != nullptr) _Cquote->Unlock();
    }
}
Cephei::QL::Instruments::CStock::CStock (boost::shared_ptr<QuantLib::Stock>& childNative, Object^ owner) : CInstrument(CStock::typeid)
{
#ifdef HANDLE
	_phStock = NULL;
#endif
	_ppStock = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppStock));
}
Cephei::QL::Instruments::CStock::CStock (QuantLib::Stock& childNative, Object^ owner) : CInstrument(CStock::typeid)
{
#ifdef HANDLE
	_phStock = NULL;
#endif
	_ppStock = new boost::shared_ptr<QuantLib::Stock> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppStock));
    _StockOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Instruments::CStock::CStock (CStock^ copy) : CInstrument(CStock::typeid)
{
#ifdef HANDLE
	_phStock = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppStock = new boost::shared_ptr<QuantLib::Stock> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppStock));
    }
}
Cephei::QL::Instruments::CStock::CStock (PLATFORM::Type^ t) : CInstrument(CStock::typeid)
{
#ifdef HANDLE
	_phStock = NULL;
#endif
	if (!t->IsSubclassOf(CStock::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CStock::CStock (QuantLib::Handle<QuantLib::Stock>& childNative, Object^ owner)  : CInstrument(CStock::typeid)
{
	_phStock = &childNative;
	_ppStock = &static_cast<boost::shared_ptr<QuantLib::Stock>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppStock));
    _StockOwner = owner;
}
Cephei::QL::Instruments::CStock::CStock (QuantLib::Handle<QuantLib::Stock> childNative)  : CInstrument(CStock::typeid)
{
	_phStock = &childNative;
	_ppStock = &static_cast<boost::shared_ptr<QuantLib::Stock>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppStock));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CStock::CStock (QuantLib::Stock childNative)  : CInstrument(CStock::typeid)
{
#ifdef HANDLE
	_phStock = NULL;
#endif
	_ppStock = new boost::shared_ptr<QuantLib::Stock> (new QuantLib::Stock (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppStock));
}
#endif

Cephei::QL::Instruments::CStock::~CStock ()
{
    if (_ppStock != NULL)
    {
	    delete _ppStock;
        _ppStock = NULL;
    }
}
Cephei::QL::Instruments::CStock::!CStock ()
{
    if (_ppStock != NULL)
    {
	    delete _ppStock;
    }
}
QuantLib::Stock& Cephei::QL::Instruments::CStock::GetReference ()
{
    if (_ppStock == NULL) throw REFNEW NativeNullException ();
	return **_ppStock;
}
boost::shared_ptr<QuantLib::Stock>& Cephei::QL::Instruments::CStock::GetShared ()
{
    if (_ppStock == NULL) throw REFNEW NativeNullException ();
	return *_ppStock;
}
QuantLib::Stock* Cephei::QL::Instruments::CStock::GetPointer ()
{
    if (_ppStock == NULL) throw REFNEW NativeNullException ();
	return &**_ppStock;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Stock>& Cephei::QL::Instruments::CStock::GetHandle ()
{
	if (_phStock == NULL)
	{
		_phStock = new Handle<QuantLib::Stock> (*_ppStock);
	}
	return *_phStock;
}
#endif
bool Cephei::QL::Instruments::CStock::HasNative () 
{
	return (_ppStock != NULL);
}

Boolean Cephei::QL::Instruments::CStock::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppStock)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IStock^ Cephei::QL::Instruments::CStock_Factory::Create (Cephei::QL::IQuote^ quote, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CStock ( quote,  QL_Pricer);
}
